<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>原型链即oop</title>
	<script type="text/javascript" defer></script>
	<script type="text/javascript" async></script>
</head>
<body>
	<script type="text/javascript">
		// var o1 = {name:'o1'}
		// var oo1 = new Object({name:'oo1'});
		// var M = function(name){this.name=name}
		// var o2 = new M('o2')
		// var p = {name:'p'}
		// var o3 = Object.create(p)
		// var new2 = function(func){
		// 	var o  = Object.create(func.prototype);
		// 	var k = func.call(o);
		// 	if(typeof k === 'object'){
		// 		return k;
		// 	}else {
		// 		return o;
		// 	}
		// }
		
		//借助构造函数实现继承
		//无法继承父类原型链上的方法
		function P1(){
			this.name = 'p1'
		}
		function C1(){
			P1.call(this);
			this.type = 'c1'
		}
		console.log(new C1().name)


		//借助原型链实现继承
		//实例间会互相干扰
		function P2(){
			this.name = 'p2'
		}
		function C2(){
			this.type = 'c2'
		}
		C2.prototype = new P2();
		console.log(new C2().name)

		//构造函数+原型实现继承
		//构造函数被执行2次
		function P3(){
			this.name = 'p3'
		}
		function C3(){
			P3.call(this);
			this.type = 'c3'
		}
		C3.prototype = new P3();
		console.log(new C3().name)

		//组合优化1
		//无法区分实例由谁直接实例化
		function P4(){
			this.name = 'p4'
		}
		function C4(){
			this.type = 'c4'
			P4.call(this)
		}
		C4.prototype = P4.prototype
		console.log(new C4().name)

		//组合优化2
		//通过Object.create方法分离父子类的原型对象
		//对子类的构造器修正
		function P5(){
			this.name ='p5'
		}
		function C5(){
			this.type = 'c5'
			P5.call(this)
		}
		C5.prototype = Object.create(P5.prototype);
		C5.prototype.constructor = C5;
		console.log(new C5().name,new C5().__proto__.constructor === C5,P5.prototype.constructor === P5)
	</script>
</body>
</html>